#include<stdio.h>
#include<stdlib.h>
#include<string.h> 

//void lack()

struct booklist{
	char Class[20];   //班级 
	int ID;           //学号 
	char name[20];    //姓名 
	char ISBN[20];    //书号 
	char press[30];   //出版社 
	double price;     //单价 
	char add[50];     //地址 	
	char title[30];   //书名 
	int number;       //数量 
	char phone[30];   //电话 
}; 

int main()
{
	booklist stu[200];
	int i = 0;
	FILE *fp;
	fp = fopen("lackbook.txt","r");
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return 0; 
	}
	while(!feof(fp))
	{
		fscanf(fp,"%d",&stu[i].ID);
		fscanf(fp,"%s",&stu[i].name);
		fscanf(fp,"%s",&stu[i].ISBN);
		fscanf(fp,"%d\n",&stu[i].number);
		i++;
	}
	fclose(fp);
	int n = i;
	printf("\n\n\n");
	printf("\t*****************************************************************\t\n");
	printf("\t*\t\t\t    缺书登记表    \t\t\t*\t\n");
	printf("\t*****************************************************************\t\n");
	printf("\t*\t学号\t\t姓名\t\t书号\t\t数量\t*\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*****************************************************************\t\n");
		printf("\t*\t%d",stu[i].ID);
		printf("\t%s",stu[i].name);
		printf("\t%s",stu[i].ISBN);
		printf("\t%d\t*\n",stu[i].number);
		printf("\t*****************************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.删除\t\t");
	printf("\n\t\t3.修改\t\t");
	printf("\n\t\t4.退出\t\t");
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)                        
	{
		system("cls"); 
		printf("\n\t学号：");
		scanf("%d",&stu[i].ID);
		printf("\t姓名：");
		scanf("%s",&stu[i].name);
		getchar();
		printf("\t书号：");
		scanf("%s",&stu[i].ISBN);
		getchar(); 
		printf("\t数量：");
		scanf("%d",&stu[i].number);
        fp = fopen("lackbook.txt","a");
		if(fp == NULL)
	    {
		    printf("未成功打开文件");
			return 0; 
	    }
        fprintf(fp,"\n%d %s %s %d",stu[i].ID,stu[i].name,stu[i].ISBN,stu[i].number);
		fclose(fp);
		printf("\n\t信息录入成功!");
    }
	if(x == 2)
	{
		system("cls");
		int j = 0;
		int id , flag = 0;
		char isbn[20];
		printf("\n\t请输入想要删除的学生的学号：");
		scanf("%d",&id);
		printf("\n\t请输入想要删除的学生的书号：");
		scanf("%s",isbn);
		getchar();
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,stu[i].ISBN) == 0&&id == stu[i].ID)
			{
				flag = 1;
				for(j=i;j<n-1;j++)
				{
					stu[j] = stu[j+1];
				}
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t没有该条记录"); 
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("lackbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return; 
	        }
	        for(i=0;i<n-1;i++)
	        {
		        fprintf(fp,"%d ",stu[i].ID);
		        fprintf(fp,"%s ",stu[i].name);
	        	fprintf(fp,"%s ",stu[i].ISBN);
		        fprintf(fp,"%d\n",stu[i].number);
		    }
	        fclose(fp);
			printf("\n\t删除成功");	
		}
	}
	if(x == 3)
	{
	    system("cls");
		int id , number , flag = 0;
		char isbn[20];
		printf("\n\t请输入想要修改的学生的学号：");
		scanf("%d",&id);
		printf("\n\t请输入想要修改的学生的书号：");
		scanf("%s",isbn);
		getchar();
		printf("\n\t请输入新数量：");
		scanf("%d",&number);
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,stu[i].ISBN) == 0&&id == stu[i].ID)
			{
				flag = 1;
				stu[i].number = number;
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t没有该条记录"); 
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("lackbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return 0; 
	        }
	        for(i=0;i<n;i++)
	        {
		        fprintf(fp,"%d ",stu[i].ID);
		        fprintf(fp,"%s ",stu[i].name);
	        	fprintf(fp,"%s ",stu[i].ISBN);
		        fprintf(fp,"%d\n",stu[i].number);
		    }
	        fclose(fp);
			printf("\n\t修改成功");	
		}	
    }
    if(x == 4)
    {
    	return 0; 
	}
	return 0;
}